/*    */ package org.yaukie.auth.service.impl;
/*    */ 
/*    */ import com.google.code.kaptcha.Producer;
/*    */ import java.awt.image.BufferedImage;
/*    */ import java.io.ByteArrayOutputStream;
/*    */ import java.io.IOException;
/*    */ import java.util.HashMap;
/*    */ import java.util.Map;
/*    */ import java.util.concurrent.TimeUnit;
/*    */ import javax.annotation.Resource;
/*    */ import javax.imageio.ImageIO;
/*    */ import org.slf4j.Logger;
/*    */ import org.slf4j.LoggerFactory;
/*    */ import org.springframework.beans.factory.annotation.Autowired;
/*    */ import org.springframework.beans.factory.annotation.Value;
/*    */ import org.springframework.stereotype.Component;
/*    */ import org.springframework.util.Base64Utils;
/*    */ import org.yaukie.auth.constant.AjaxResult;
/*    */ import org.yaukie.auth.service.api.CaptchaHandlerService;
/*    */ import org.yaukie.base.redis.RedisOrMapCache;
/*    */ import org.yaukie.base.uuid.IdUtils;
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ @Component
/*    */ public class CaptchaHandlerServiceImpl
/*    */   implements CaptchaHandlerService
/*    */ {
/* 35 */   private static final Logger log = LoggerFactory.getLogger(CaptchaHandlerServiceImpl.class);
/*    */ 
/*    */   
/*    */   @Resource(name = "captchaProducer")
/*    */   private Producer captchaProducer;
/*    */ 
/*    */   
/*    */   @Resource(name = "captchaProducerMath")
/*    */   private Producer captchaProducerMath;
/*    */ 
/*    */   
/*    */   @Value("${yaukie.captcha.type:math}")
/*    */   private String captchaType;
/*    */ 
/*    */   
/*    */   @Value("${yaukie.captcha.expired:1}")
/*    */   private String captchaExpired;
/*    */   
/*    */   @Autowired
/*    */   private RedisOrMapCache redisCache;
/*    */ 
/*    */   
/*    */   public AjaxResult getVeriCode() {
/* 58 */     String uuid = IdUtils.fastSimpleUUID();
/*    */     
/* 60 */     String capchaCodeKey = "captcha_codes:" + uuid;
/* 61 */     String code = "";
/* 62 */     String text = "";
/* 63 */     BufferedImage image = null;
/*    */     
/* 65 */     if (this.captchaType.equalsIgnoreCase("") || this.captchaType.equalsIgnoreCase("math")) {
/* 66 */       text = this.captchaProducerMath.createText();
/* 67 */       code = text.substring(text.lastIndexOf("@") + 1);
/* 68 */       image = this.captchaProducerMath.createImage(text.substring(0, text.lastIndexOf("@")));
/*    */     } else {
/* 70 */       code = this.captchaProducer.createText();
/* 71 */       image = this.captchaProducer.createImage(code);
/*    */     } 
/*    */ 
/*    */     
/* 75 */     this.redisCache.setCacheObject(capchaCodeKey, code, Integer.valueOf(Integer.parseInt(this.captchaExpired)), TimeUnit.MINUTES);
/* 76 */     ByteArrayOutputStream fbao = new ByteArrayOutputStream();
/* 77 */     AjaxResult ajaxResult = AjaxResult.success();
/*    */     try {
/* 79 */       ImageIO.write(image, "jpg", fbao);
/* 80 */     } catch (IOException e) {
/* 81 */       return AjaxResult.error(9001, e.getMessage());
/*    */     } 
/*    */     
/* 84 */     Map dataMap = new HashMap();
/* 85 */     dataMap.put("uuid", uuid);
/* 86 */     dataMap.put("code", new String(Base64Utils.encode(fbao.toByteArray())));
/* 87 */     ajaxResult.put("data", dataMap);
/* 88 */     return ajaxResult;
/*    */   }
/*    */ 
/*    */ 
/*    */   
/* 93 */   public boolean verifyCode() { return true; }
/*    */ }


/* Location:              /Users/rex/Desktop/x-common-auth-1.0.0-SNAPSHOT.jar!/org/yaukie/auth/service/impl/CaptchaHandlerServiceImpl.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       1.0.7
 */